//
//  LKTaiQianManagerViewController.m
//  Mideleague_Shop
//
//  Created by Zhiwang on 2020/6/30.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKTaiQianManagerViewController.h"
#import "LKBaseModel.h"
#import "LKTaiQianEditViewController.h"
#import "LKTaiqianModel.h"
#import "TypeMenu.h"
#import "LKLockScanViewController.h"
@interface LKDefaultCell : LKBaseTableCell

@property(nonatomic,strong)LKBaseModel *model;

@end


@implementation LKDefaultCell

-(void)setModel:(LKBaseModel *)model{
    self.textLabel.text = @"默认台前";
    self.detailTextLabel.text = @"今日收入";
    self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

@end

@interface LKTaiQianManagerViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)LKBaseTableview *tableView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,assign)NSInteger page;
@property (nonatomic,strong)TypeMenu *typeMenu;
@end

@implementation LKTaiQianManagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 0;
    self.navigationItem.title = @"台签管理";
    [self setNavgationRightBtn];
    [self createTableview];
    [self getCardDataWithPage];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(headerReloadData) name:@"TaiqianPageReload" object:nil];
    // Do any additional setup after loading the view.
}
-(void)setNavgationRightBtn{
    WS(weakSelf)
    LKBaseButton *rightBtn = [LKBaseButton buttonWithFrame:CGRectMake(0, 0, 64, 24) type:UIButtonTypeCustom title:@"添加台签" titleColor:COLOR_HEX(0x333333) layercorne:13 font:[UIFont fontWithName:@"PingFangSC-Regular" size: 16] backgroudColor:[UIColor clearColor] AndActionBlock:^(UIButton * _Nonnull btn) {
        [weakSelf.typeMenu showMenu];
//        LKTaiQianEditViewController *vc = [[LKTaiQianEditViewController alloc] init];
//        vc.operateStyle = LKTaiqianOperateStyleAdd;
//        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}
- (TypeMenu *)typeMenu{
    if (!_typeMenu) {
        WS(weakSelf)
        NSArray *dataArray = [[NSArray alloc]initWithObjects:
           [NSDictionary dictionaryWithObjectsAndKeys:@"扫码添加",@"title",@"motify",@"img", nil],
                              [NSDictionary dictionaryWithObjectsAndKeys:@"手动添加",@"title",@"motify",@"img", nil], nil];
        _typeMenu = [TypeMenu createMenuWithFrame:CGRectMake(WIDTH - 120,NAVIGATION_BAR_HEIGHT, 100, 100) dataArray:dataArray];
        _typeMenu.selectRowBlcok = ^(NSString * _Nonnull title) {
            if ([title isEqualToString:@"手动添加"]) {
                LKTaiQianEditViewController *vc = [[LKTaiQianEditViewController alloc] init];
                vc.operateStyle = LKTaiqianOperateStyleAdd;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }else{
                [weakSelf chooseErweima];
            }
        };
    }
    return _typeMenu;
}
-(void)chooseErweima{
    LBXScanViewStyle *style = [[LBXScanViewStyle alloc]init];
    style.centerUpOffset = 44;
    style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle_Inner;
    style.photoframeLineW = 3;
    style.photoframeAngleW = 18;
    style.photoframeAngleH = 18;
    style.isNeedShowRetangle = NO;
    style.anmiationStyle = LBXScanViewAnimationStyle_LineMove;
    //qq里面的线条图片
    UIImage *imgLine = [UIImage imageNamed:@"qrcode_scan_light_green"];
    style.animationImage = imgLine;
    style.notRecoginitonArea = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    LKLockScanViewController *vc = [[LKLockScanViewController alloc] init];
    vc.style = style;
    vc.isOpenInterestRect = YES;
    vc.libraryType = SLT_Native;
    vc.scanType = LKScanTypeScanTaiqian;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)createTableview{
    self.tableView = [LKBaseTableview setTableviewWithFrame:CGRectZero style:UITableViewStyleGrouped registercellClasss:@[@"LKBankListCell"]];
    [self.tableView registerClass:[LKDefaultCell class] forCellReuseIdentifier:@"LKDefaultCell"];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = viewDefualtColor;
    [self.view addSubview:_tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(NAVIGATION_BAR_HEIGHT + 10);
        make.bottom.equalTo(self.view).offset(0);
    }];

    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerReloadData)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerReloadData)];
    [self.tableView reloadData];
}

/// 下拉刷新
-(void)headerReloadData{
    self.page = 0;
    self.tableView.mj_footer.hidden = NO;
    [self getCardDataWithPage];
}
/// 上拉加载更多
-(void)footerReloadData{
    self.page++;
    [self getCardDataWithPage];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellId = @"cellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    LKTaiqianModel *model = self.dataArray[indexPath.section];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = model.cate_name;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"今日收入￥%@",model.total_pay];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60.0f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [[UIView alloc] init];
    return header;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1.0f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] init];
    return footerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0000000001f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LKTaiqianModel *model = self.dataArray[indexPath.section];
    LKTaiQianEditViewController *vc = [[LKTaiQianEditViewController alloc] init];
    vc.operateStyle = LKTaiqianOperateStyleEdit;
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
/// 获取台签列表数据
-(void)getCardDataWithPage{
    NSMutableDictionary *param = [[NSMutableDictionary alloc] initWithDictionary:@{@"page":@(self.page)}];
    NSString *sign = [NSString encoingWithDic:param];
    [param setObject:sign forKey:@"sign"];
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyTaiqianCarlistUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {

        if ([responseObject[@"code"] isEqualToString:@"success"]) {
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            if (self.page == 0) {
                [self.dataArray removeAllObjects];
            }
            NSArray *dicArray = responseObject[@"data"];
            if (dicArray.count == 0) {
                self.tableView.mj_footer.hidden = YES;
            }
            for (NSDictionary *dic in dicArray) {
                LKTaiqianModel *model = [LKTaiqianModel modelWithDictionary:dic];
                [self.dataArray addObject:model];
            }
            [self.tableView reloadData];
        }else{
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
        }
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
